﻿using System;
using Framework.MVC;
using Log.Model;
using Identity.Model;
using Account.Model;
using Identity.IService.Entry;
using Common.IService;
using Framework.MVVM.Model;
using Common.Service;
using Log.Service;
using Log.IService;
using Account.IService;
using Account.Service;
using Identity.IService;
using Identity.Service;
using FrontCenter.Entry;
using Account.Model.Behavior;

namespace FrontCenter.Models
{
    [ServiceAttribute(typeof(IVerifyCodeService), typeof(VerifyCodeService), "Instance")]
    [ServiceAttribute(typeof(IAccountService), typeof(AccountService), "Instance")]
    [ServiceAttribute(typeof(IHashService), typeof(HashService), "Instance")]
    [ServiceAttribute(typeof(ISaltService), typeof(SaltService), "Instance")]
    [ServiceAttribute(typeof(IIdentityService), typeof(IdentityService), "Instance")]
    [ServiceAttribute(typeof(ILogService), typeof(LogService), "Instance")]
    [ServiceAttribute(typeof(ICodeService), typeof(CodeService), "Instance")]
    public class NewAccountModel : AsyncModelBase, IWriteLog, IValidateVerifyCode, IDeleteVerifyCode, INewAccount, INewIdentity
    {
        #region Field

        private const string m_verifycode_empty = "naccount0";
        private const string m_verifycode_error = "naccount1";
        private const string m_verifycode_complete = "naccount2";
        private const string m_verifyvalue_error = "naccount3";
        private const string m_verifyvalue_complete = "naccount4";
        private const string m_account_error = "naccount5";
        private const string m_account_complete= "naccount6";
        private const string m_identity_error = "naccount7";
        private const string m_identity_complete = "naccount8";

        private const string m_reg_log = "reg_log00";

        private const string m_resource = "vc00";
        private const string m_user_resource = "u000";
        private const string m_entry = "entry";
        private const string m_error = "error";
        private const char m_split = ';';
        
        private const string m_errors_verifycode = "verifycode";
        

        #endregion Field

        #region AsyncModelBase

        protected override void OnInit(Type type)
        {
            this.OnBeginRequest();
            this.ValidateVerifyCode(m_resource, m_verifycode_error, m_verifyvalue_complete);
        }

        #endregion AsyncModelBase

        #region IWriteLog

        public void OnWriteLogComplete(string eventCode)
        {
            this.OnEndRequest();
        }

        public void OnWriteLogError(string eventCode)
        {
            this.OnEndRequest();
        }

        #endregion IWriteLog

        #region IValidateVerifyCode

        public void OnValidateVerifyCodeComplete(string key, bool entry)
        {
            this.DeleteVerifyCode(m_resource,string.Empty,string.Empty);
        }

        public void OnValidateVerifyCodeError(string key)
        {
            OnComplete(m_error, m_errors_verifycode);
            this.Log(key, new IdentityEntry { code = VerifyCode, source = Source, resource = m_resource, value = VerifyValue });
        }

        public string Source { get; set; }

        public string VerifyCode { get; set; }

        public string VerifyValue { get; set; }

        #endregion IValidateVerifyCode

        #region IDeleteVerifyCode

        public void OnDeleteVerifyCodeComplete(string key, bool result)
        {
            this.NewAccount(AccountResource,m_account_error, m_account_complete);
        }

        public void OnDeleteVerifyCodeError(string key)
        {
            this.NewAccount(AccountResource,m_account_error, m_account_complete);
        }

        #endregion IDeleteVerifyCode

        #region INewAccount

        public string AccountResource { get; set; }

        public string AccountCode { get; set; }

        public string AccountName { get; set; }

        public string AccountPassword { get; set; }

        public string Mark { get; set; }

        public string Address { get; set; }

        public string SourceCode { get; set; }

        public void OnNewAccountComplete(string key, string code)
        {
            OnBeginRequest();
            this.Log(key, Source + m_split + AccountName);

            IdentityResource = m_user_resource;
            IdentityValue = code;
            this.NewIdentity(m_identity_error, m_identity_complete);
        }

        public void OnNewAccountError(string key)
        {
            this.Log(key, Source + m_split + AccountName);
        }

        #endregion INewAccount

        #region INewIdentity

        public string IdentityCode { get; set; }

        public string IdentityResource { get; set; }

        public string IdentityValue { get; set; }

        public void OnNewIdentityComplete(string key, IdentityEntry entry)
        {
            OnComplete(m_entry, entry);
            this.Log(key, entry);
        }

        public void OnNewIdentityError(string key)
        {
            this.Log(key, new IdentityEntry { code = IdentityCode, source = Source, resource = IdentityResource, value = IdentityValue });
        }

        #endregion INewIdentity

    }
}